﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM.Framework;
using System.Globalization;
using System.Runtime.InteropServices;

namespace ApexInterest
{
//    public class CoFinancial
//    {
//        public SAPbouiCOM.Application SBO_Application;
//        public SAPbobsCOM.Company Ocompany;

//public New(SAPbobsCOM.Company ocompany1, SAPbouiCOM.Application sbo_application1)
//{
//    SBO_Application = sbo_application1;
//    Ocompany = ocompany1;
//}
//        //**********************************************************
//        // declaring an Event filters container object and an
//        // event filter object
//        //**********************************************************

//             private void SBO_Application_ItemEvent(string FormUID, ref SAPbouiCOM.ItemEvent pVal, out bool BubbleEvent)
//        {
//            BubbleEvent = true;

//        }

   
    
    

//    }

    public class CoFinancial
    {
        CultureInfo cultureInfo;
        SAPbouiCOM.Application SBO_Application;
        SAPbobsCOM.Company Ocompany;
        SAPbouiCOM.EditText oEdit;
        SAPbouiCOM.Item oItem;
        SAPbouiCOM.ComboBox oCombo;
        SAPbouiCOM.ComboBox oCombo1;
        SAPbouiCOM.CheckBox oCheck;

        public CoFinancial(SAPbobsCOM.Company ocompany1, SAPbouiCOM.Application sbo_application1)
        {
            SBO_Application = sbo_application1;
            Ocompany = ocompany1;
            SBO_Application.ItemEvent += new SAPbouiCOM._IApplicationEvents_ItemEventEventHandler(SBO_Application_ItemEvent); 
        }

        private void CO_CalculateInterest(bool isChangeAdminFee, SAPbouiCOM.Form oForm)
        {
            try
            {
                double TotalDealAmt = 0, DisFee = 0, IntrestProfit = 0, DailyJE = 0, LastJE = 0, IntrestProfitPartner;
                int term1 = 0;
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("10"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                TotalDealAmt = double.Parse(oEdit.Value);
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("25"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                DisFee = double.Parse(oEdit.Value);
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("12"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                if (oEdit.String == "")
                {
                    return;
                }
                term1 = Int32.Parse(oEdit.String);

                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("35"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                IntrestProfitPartner = double.Parse(oEdit.Value);
                
                IntrestProfit = (((TotalDealAmt * DisFee) / (30 * 100)) * term1) - IntrestProfitPartner;
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("19"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                oEdit.Value = IntrestProfit.ToString(CultureInfo.InvariantCulture);
                DailyJE = IntrestProfit / term1;
                DailyJE = Math.Round(DailyJE, 2);
                LastJE = (IntrestProfit - (DailyJE * term1));
                LastJE = Math.Round(LastJE, 2);
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("21"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                oEdit.Value = DailyJE.ToString(CultureInfo.InvariantCulture);
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("23"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                oEdit.Value = LastJE.ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception ex)
            {

            }
        }

        private void New_CO_CalculateInterest(bool isChangeAdminFee, SAPbouiCOM.Form oForm)
        {
            try
            {
                oForm.Freeze(true);

                double TotalDealAmt = 0, DisFee = 0, IntrestProfit = 0, DailyJE = 0, LastJE = 0;
                int term1 = 0;



                double NewTotalProfitCreditedAmt = 0, NewDisFee = 0, NewIntrestProfit = 0, TotJEamt = 0, NewDailyJE = 0, NewDailyJE1 = 0, NewLastJE = 0, NewLastJE1 = 0, Daydiff = 0;
                int NewTermsDays = 0;


                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("42"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                if (oEdit.String == "")
                {
                    return;
                }
                string st = oEdit.String;
                DateTime SDt;
                SDt = Convert.ToDateTime(st);
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("14"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                if (oEdit.String == "")
                {
                    return;
                }
                string Est = oEdit.String;
                DateTime EDt;
                EDt = Convert.ToDateTime(Est);

                Daydiff = (EDt - SDt).TotalDays;
                Daydiff = Daydiff - 1;

                DailyJE = double.Parse(oEdit.Value);

                //50

                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("50"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                oEdit.String = (DailyJE * Daydiff).ToString(CultureInfo.InvariantCulture);//NewTotalProfitCreditedAmt

                editText1 = (SAPbouiCOM.EditText)this.GetItem("Item_99").Specific;
                TotalDealAmt = double.Parse(editText1.Value);
                NewDisFee = double.Parse(EditText56.Value);
                NewTermsDays = Int32.Parse(EditText54.Value);
                NewIntrestProfit = (((TotalDealAmt * NewDisFee) / 100) / 30) * NewTermsDays;
                NewIntrestProfit = NewIntrestProfit - double.Parse(EditText67.Value);
                EditText57.String = NewIntrestProfit.ToString(CultureInfo.InvariantCulture);

                EditText64.String = NewIntrestProfit.ToString(CultureInfo.InvariantCulture);
                TotJEamt = NewIntrestProfit;
                NewDailyJE = (TotJEamt / NewTermsDays);
                NewDailyJE1 = Math.Round(NewDailyJE, 2);
                EditText65.String = NewDailyJE1.ToString(CultureInfo.InvariantCulture);
                NewLastJE = (TotJEamt - (NewDailyJE * NewTermsDays));
                NewLastJE1 = Math.Round(NewLastJE, 2);
                EditText66.String = NewLastJE1.ToString(CultureInfo.InvariantCulture);
                //  NewDailyJE.String = Dt.ToString("dd/MM/yy");

            }
            catch (Exception ex)
            {
                // Application.SBO_Application.MessageBox(ex.Message);
            }
            finally
            {
                oForm.Freeze(false);
            }

        }
        private void PartnerAdvance_CalculateInterest(bool isChangeAdminFee, SAPbouiCOM.Form oForm)
        {
            try
            {

                double RefundAmt = 0, DisFee = 0;
                int term1 = 0, term2 = 0;
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("33"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                if (oEdit.String == "")
                {
                    return;
                }
                term1 = Int32.Parse(oEdit.String);

                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("12"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                if (oEdit.String == "")
                {
                    return;
                }
                term2 = Int32.Parse(oEdit.String);

                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("29"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                RefundAmt = double.Parse(oEdit.Value);
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("31"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                DisFee = double.Parse(oEdit.Value);
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("35"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                oEdit.Value = ((RefundAmt * DisFee / (100 * term1)) * term2).ToString(CultureInfo.InvariantCulture);                           
            }
            catch (Exception ex)
            {
                Application.SBO_Application.MessageBox(ex.Message);
            }
            finally
            {
               // this.UIAPIRawForm.Freeze(false);
            }

        }
        private void New_PartnerAdvance_CalculateInterest(bool isChangeAdminFee, SAPbouiCOM.Form oForm)
        {
            try
            {

                double RefundAmt = 0, DisFee = 0;
                int term1 = 0, term2 = 0;
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("64"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                if (oEdit.String == "")
                {
                    return;
                }
                term1 = Int32.Parse(oEdit.String);

                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("40"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                if (oEdit.String == "")
                {
                    return;
                }
                term2 = Int32.Parse(oEdit.String);

                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("60"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                RefundAmt = double.Parse(oEdit.Value);
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("62"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                DisFee = double.Parse(oEdit.Value);
                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("66"));
                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                oEdit.Value = ((RefundAmt * DisFee / (100 * term1)) * term2).ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception ex)
            {
                Application.SBO_Application.MessageBox(ex.Message);
            }
            finally
            {
                // this.UIAPIRawForm.Freeze(false);
            }

        }
        private void SBO_Application_ItemEvent(string FormUID, ref SAPbouiCOM.ItemEvent pVal, out bool BubbleEvent)
        {

            BubbleEvent = true;

            // *************************************************************************
            //  BubbleEvent sets the behavior of SAP Business One.
            //  False means that the application will not continue processing this event.
            // *************************************************************************

            if (pVal.FormUID == "ABCOFINANCE")
            {
                

                BubbleEvent = true;
                // Choose from List
                if (pVal.EventType == SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST)
                {
                    SAPbouiCOM.IChooseFromListEvent oCFLEvento = null;
                    oCFLEvento = ((SAPbouiCOM.IChooseFromListEvent)(pVal));
                    string sCFL_ID = null;
                    sCFL_ID = oCFLEvento.ChooseFromListUID;
                    SAPbouiCOM.Form oForm = null;
                    oForm = SBO_Application.Forms.Item(FormUID);
                    SAPbouiCOM.ChooseFromList oCFL = null;
                    oCFL = oForm.ChooseFromLists.Item(sCFL_ID);
                    if (oCFLEvento.BeforeAction == false)
                    {
                        try
                        {
                            SAPbouiCOM.DataTable oDataTable = null;
                            oDataTable = oCFLEvento.SelectedObjects;
                            string val = null;
                            try
                            {
                                val = System.Convert.ToString(oDataTable.GetValue(0, 0));
                            }
                            catch (Exception ex)
                            {

                            }
                            if ((pVal.ItemUID == "84") | (pVal.ItemUID == "Button"))
                            {
                               
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("84"));
                                oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                                oEdit.String = val;
                                //  oForm.DataSources.UserDataSources.Item( "EditDS" ).ValueEx = val; 
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }

                //*********************et_ITEM_PRESSED
                if (pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED && pVal.BeforeAction == false && pVal.InnerEvent == false)
                {

                    try
                    {
                        SAPbouiCOM.Form oForm = null;
                        oForm = SBO_Application.Forms.Item(FormUID);
                        if (pVal.ItemUID == "70" && (pVal.FormMode == 1 || pVal.FormMode == 2))
                        {
                            oItem = (SAPbouiCOM.Item)(oForm.Items.Item("70"));
                            oCheck = ((SAPbouiCOM.CheckBox)(oItem.Specific));
                            if (oCheck.Checked == true)
                            {
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("40"));
                                oItem.Enabled = true;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("42"));
                                oItem.Enabled = true;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("44"));
                                oItem.Enabled = true;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("60"));
                                oItem.Enabled = true;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("62"));
                                oItem.Enabled = true;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("64"));
                                oItem.Enabled = true;
                            }
                            else
                            {
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("40"));
                                oItem.Enabled = false;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("42"));
                                oItem.Enabled = false;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("44"));
                                oItem.Enabled = false;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("60"));
                                oItem.Enabled = false;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("62"));
                                oItem.Enabled = false;
                                oItem = (SAPbouiCOM.Item)(oForm.Items.Item("64"));
                                oItem.Enabled = false;
                            }


                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }

            //*********************Combo Slect
            if (pVal.EventType == SAPbouiCOM.BoEventTypes.et_COMBO_SELECT && pVal.Before_Action==false && pVal.InnerEvent==false)
            {
                SAPbouiCOM.Form oForm = null;
                oForm = SBO_Application.Forms.Item(FormUID);
                if (pVal.ItemUID == "1000002" && pVal.ItemChanged)
                {
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("1000002"));
                    oCombo = ((SAPbouiCOM.ComboBox)(oItem.Specific));
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("1000001"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    oEdit.String = oCombo.Selected.Description;
                }
                if (pVal.ItemUID == "1000003" && pVal.ItemChanged)
                {
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("1000003"));
                    oCombo = ((SAPbouiCOM.ComboBox)(oItem.Specific));
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("72"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    oEdit.String = oCombo.Selected.Description;
                }
            }

                //*********************Validate
            if (pVal.EventType == SAPbouiCOM.BoEventTypes.et_VALIDATE && pVal.Before_Action == false && pVal.InnerEvent == false)
            {
                SAPbouiCOM.Form oForm = null;
                oForm = SBO_Application.Forms.Item(FormUID);
                if (pVal.ItemUID == "8" && pVal.ItemChanged)//Outgoing Amount
                {
                    double TotalDealAmt = 0, OutgoingAmt = 0;
                    
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("8"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    OutgoingAmt = double.Parse(oEdit.Value);
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("10"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    TotalDealAmt = double.Parse(oEdit.Value);
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("29"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    oEdit.Value = (TotalDealAmt-OutgoingAmt).ToString(CultureInfo.InvariantCulture);
                   
                }
                if (pVal.ItemUID == "10" && pVal.ItemChanged)//Total Deal Amount
                {
                    double TotalDealAmt = 0, OutgoingAmt = 0;
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("8"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    OutgoingAmt = double.Parse(oEdit.Value);
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("10"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    TotalDealAmt = double.Parse(oEdit.Value);
                    if (TotalDealAmt <= OutgoingAmt)
                    {
                        Application.SBO_Application.MessageBox("Total Deal Amount can't Be Less than Outgoing Amount");
                        oEdit.Value = OutgoingAmt.ToString(CultureInfo.InvariantCulture);
                        TotalDealAmt = double.Parse(oEdit.Value);
                        oItem = (SAPbouiCOM.Item)(oForm.Items.Item("29"));
                        oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                        oEdit.Value = (TotalDealAmt - OutgoingAmt).ToString(CultureInfo.InvariantCulture);
                        return;
                    }
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("29"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    oEdit.Value = (TotalDealAmt - OutgoingAmt).ToString(CultureInfo.InvariantCulture);
                    PartnerAdvance_CalculateInterest(true, oForm);
                    CO_CalculateInterest(true, oForm);
                   
                }

                if (pVal.ItemUID == "12" && pVal.ItemChanged)//Terms
                {
                    try
                    {
                        oItem = (SAPbouiCOM.Item)(oForm.Items.Item("12"));
                        oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                        if (oEdit.String == "")
                        {
                            return;
                        }
                        int Terms;
                        Terms = Int32.Parse(oEdit.String);
                        if (Terms == 0)
                        {
                            return;
                        }
                        else
                        {
                            oItem = (SAPbouiCOM.Item)(oForm.Items.Item("14"));
                            oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                            if (oEdit.String == "")
                            {
                                return;
                            }
                            string st = oEdit.String;
                            DateTime Dt;
                            Dt = Convert.ToDateTime(st);
                           Terms = Terms - 1;
                            Dt = Dt.AddDays(Terms);
                            oItem = (SAPbouiCOM.Item)(oForm.Items.Item("17"));
                            oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                            oEdit.String = Dt.ToString("dd/MM/yy");
                            PartnerAdvance_CalculateInterest(true, oForm); 
                            CO_CalculateInterest(true, oForm);   
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                if (pVal.ItemUID == "14" && pVal.ItemChanged)//Start Date
                {
                    try
                    {
                        oItem = (SAPbouiCOM.Item)(oForm.Items.Item("12"));
                        oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                        if (oEdit.String == "")
                        {
                            return;
                        }
                        int Terms;
                        Terms = Int32.Parse(oEdit.String);
                        if (Terms == 0)
                        {
                            return;
                        }
                        else
                        {
                            oItem = (SAPbouiCOM.Item)(oForm.Items.Item("14"));
                            oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                            if (oEdit.String == "")
                            {
                                return;
                            }
                            string st = oEdit.String;
                            DateTime Dt;
                            Dt = Convert.ToDateTime(st);
                            Terms = Terms - 1;
                            Dt = Dt.AddDays(Terms);
                            oItem = (SAPbouiCOM.Item)(oForm.Items.Item("17"));
                            oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                            oEdit.String = Dt.ToString("dd/MM/yy");
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                if (pVal.ItemUID == "25" && pVal.ItemChanged)//Discount Fee %
                {
                    PartnerAdvance_CalculateInterest(true, oForm); 
                    CO_CalculateInterest(true, oForm);                 
                }
                if (pVal.ItemUID == "31" && pVal.ItemChanged)//Discount Fee % Parner
                {
                    PartnerAdvance_CalculateInterest(true, oForm);
                    CO_CalculateInterest(true, oForm);
                }
                if (pVal.ItemUID == "33" && pVal.ItemChanged)//Days
                {
                    PartnerAdvance_CalculateInterest(true, oForm);
                    CO_CalculateInterest(true, oForm);
                }

                if (pVal.ItemUID == "60" && pVal.ItemChanged)//refunding Amount partner new
                {
                   
                    New_PartnerAdvance_CalculateInterest(true, oForm);
                    New_CO_CalculateInterest(true, oForm);
                }
                if (pVal.ItemUID == "62" && pVal.ItemChanged)//Discount Fee % Parner new
                {

                    New_PartnerAdvance_CalculateInterest(true, oForm);
                    New_CO_CalculateInterest(true, oForm);
                }
                if (pVal.ItemUID == "64" && pVal.ItemChanged)//Days refunding Parner new
                {

                    New_PartnerAdvance_CalculateInterest(true, oForm);
                    New_CO_CalculateInterest(true, oForm);
                }

                if (pVal.ItemUID == "84" )//Customer Name
                {
                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("84"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    SAPbobsCOM.Recordset oRecordSet;
                    oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                    oRecordSet.DoQuery("SELECT T0.[CardName] FROM OCRD T0 WHERE T0.[CardCode] ='" + oEdit.String + "'");

                    oItem = (SAPbouiCOM.Item)(oForm.Items.Item("86"));
                    oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
                    oEdit.String = oRecordSet.Fields.Item(0).Value;//oRecordSet.Fields.Item("CardName").Value;
                  

                }
            }
                //*********************
            }

        }

        

    }

}
